#include "gameselect.h"
#include "gamehall.h"
#include "gameroom.h"

#include<QIcon>
#include<QPushButton>
#include<QSound>
#include<QPainter>
#include<QFont>
#include<QTextEdit>
#include<QFile>
#include<QTextStream>


GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(1000,800);
    this->setWindowIcon(QIcon(":res/ico.png"));
    this->setWindowTitle("游戏关卡选择");

    QPushButton *backbtn = new QPushButton(this);
    backbtn->move(900,750);
    backbtn->setIcon(QIcon(":res/back.png"));

    connect(backbtn,&QPushButton::clicked,[=](){
        this->close();
        GameHall *gameHall = new GameHall;
        gameHall->show();

        QSound::play(":res/clicked.wav");
    });


    QFont  font("华文行楷",24);

    GameRoom *gameroom = new GameRoom;



    QPushButton *simpbtn = new QPushButton(this);
    simpbtn->move(400,150);
    simpbtn->setText("简单模式");
    simpbtn->setFont(font);
    connect(simpbtn,&QPushButton::clicked,[=](){
        this->close();
        gameroom->setGeometry(this->geometry());
        gameroom->show();
        QSound::play(":res/clicked.wav");

        gameroom->setTimeout(300);
    });


    QPushButton *normalBtn = new QPushButton(this);
    normalBtn->move(400,250);
    normalBtn->setText("正常模式");
    normalBtn->setFont(font);
    connect(normalBtn,&QPushButton::clicked,[=](){
        this->close();
        gameroom->setGeometry(this->geometry());
        gameroom->show();
        QSound::play(":res/clicked.wav");

        gameroom->setTimeout(200);
    });


    QPushButton *hardBtn = new QPushButton(this);
    hardBtn->move(400,350);
    hardBtn->setText("困难模式");
    hardBtn->setFont(font);
    connect(hardBtn,&QPushButton::clicked,[=](){
        this->close();
        gameroom->setGeometry(this->geometry());
        gameroom->show();
        QSound::play(":res/clicked.wav");

        gameroom->setTimeout(100);
    });



    QPushButton *hisBtn = new QPushButton(this);
    hisBtn->move(400,450);
    hisBtn->setText("历史战绩");
    hisBtn->setFont(font);

    connect(hisBtn,&QPushButton::clicked,[=](){
        QWidget *widget = new QWidget;
        widget->setWindowTitle("历史战绩");
        widget->setFixedSize(500,300);

        QTextEdit *edit = new QTextEdit(widget);
        edit->setFont(font);
        edit->setFixedSize(500,300);

        QFile file("C:\\Users\\86187\\Desktop\\Snake\\history\\score.txt");
        file.open(QIODevice::ReadOnly);

        QTextStream in(&file);
        int data = in.readLine().toInt();

        edit->append("得分为:");
        edit->append(QString::number(data));

        widget->show();

    });


}

void GameSelect::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix(":res/game_select.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

}
